/*
 * To change this template, choose Tools | Templates
 * and open the template in the editor.
 */
package SingleThreaded;

import Vue.Vue;
import java.util.Random;

/**
 *
 * @author michaelrepiso
 */
public class MonteCarloSimulation {

    /**
     *
     * @param CallPutFlag
     * @param S
     * @param X
     * @param T
     * @param r
     * @param b
     * @param v
     * @param nSteps
     * @param nSimulations
     * @return
     */
    public Double MonteCarloStandardOption(String CallPutFlag, Double S, Double X, Double T,
            Double r, Double b, Double v, int nSteps, int nSimulations, Vue vue) {

        //declaration ressources
        double dt, St;
        double Sum = 0, Drift, vSqrdt;
        int i, j, z;
        
        //vue.setProgressBar(new JProgressBar(0,(5+nSimulations)));
        //vue.getMilieu().add(vue.getProgressBar(), java.awt.BorderLayout.SOUTH);
        vue.getProgressBar().setStringPainted(true);
        vue.getProgressBar().setIndeterminate(false);
        vue.getProgressBar().setValue(0);

        //calculs
        dt = T / nSteps;
        vue.getProgressBar().setValue((1*100)/(5+nSimulations));
        vue.getProgressBar().setString(String.valueOf((1*100)/(5+nSimulations)) + " %");
        Drift = (b - Math.pow(v, 2) / 2) * dt;
        vue.getProgressBar().setValue((2*100)/(5+nSimulations));
        vue.getProgressBar().setString(String.valueOf((2*100)/(5+nSimulations)) + " %");
        vSqrdt = v * Math.sqrt(dt);
        vue.getProgressBar().setValue((3*100)/(5+nSimulations));
        vue.getProgressBar().setString(String.valueOf((3*100)/(5+nSimulations)) + " %");
        //callputflag condition
        switch (CallPutFlag) {
            case "c":
                z = 1;
                break;
            case "p":
                z = -1;
                break;
            default:
                z = 0;
                break;
        }
        vue.getProgressBar().setValue((5*100)/(5+nSimulations));
        vue.getProgressBar().setString(String.valueOf((5*100)/(5+nSimulations)) + " %");

        Random rando = new Random();
        for (i = 1; i <= nSimulations; i++) {
            St = S;

            for (j = 1; j <= nSteps; j++) {
                St = St * Math.exp(Drift + vSqrdt * rando.nextGaussian());
            }
            Sum = Sum + Math.max(z * (St - X), 0);
            vue.getProgressBar().setValue(((5+i)*100)/(5+nSimulations));
            vue.getProgressBar().setString(String.valueOf(((5+i)*100)/(5+nSimulations)) + " %");
        }
        
        vue.getProgressBar().setValue(vue.getProgressBar().getMinimum());
        vue.getProgressBar().setString("");
        
        
        
        return Math.exp(-r * T) * (Sum / nSimulations);
    }
    
    
}
